package com.bj58.ecdata.webframework;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author lichao
 */
@Controller
public class BaseController {
	static Logger LOG = LoggerFactory.getLogger(BaseController.class);

	@RequestMapping("/ping")
	@ResponseBody
	public String test() {
		return "pong";
	}

	@ExceptionHandler
	@ResponseBody
	public Exception exp(Exception ex, HttpServletRequest request) {
		if (ex instanceof HttpMessageNotReadableException) {// json读取错误
			LOG.warn("parse request json error", ex);
		} else if (ex instanceof MissingServletRequestParameterException) {// param-missing
			LOG.warn("request params missing", ex);
		} else {
			LOG.error("", ex);
		}
		return ex;
	}

}
